package com.learn.jdk.practice.charter1;

import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;



/**
 * @author: lipan
 * @date: 2021/4/24 11:10
 */

public class PraCase {


    List<Transaction> transactions;

    @Before
    public void initData() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

    /**
     * 找出2011年的所有交易并按交易额排序（从低到高）
     */

    @Test
    public void test1() {

        List<Transaction> collect = transactions.stream().filter(t -> t.getYear() == 2011).sorted(Comparator.comparingInt(Transaction::getValue)).collect(Collectors.toList());
        List<Transaction> collect2 = transactions.stream().filter(t -> t.getYear() == 2011).sorted(Comparator.comparing(Transaction::getValue)).collect(Collectors.toList());

        System.out.println(collect2);
        collect.forEach(System.out::println);
    }

    /**
     * 交易员都在哪些不同的城市工作过
     */
    @Test
    public void test2() {
        transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 查找所有来自于剑桥的交易员，并按姓名排序
     */
    @Test
    public void test3() {
        List<Trader> collect = transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());

        collect.forEach(System.out::println);

    }

    /**
     * 返回所有交易员的姓名字符串，按字母顺序排序
     */
    @Test
    public void test4(){
        String collect = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.joining());

        System.out.println(collect);
    }

    /**
     * 有没有交易员是在米兰工作的
     */
    @Test
    public void test5(){
        boolean b = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .anyMatch(city -> "Milan".equals(city));

        System.out.println(b);
    }

    /**
     * 打印生活在剑桥的交易员的所有交易额
     */
    @Test
    public void test6(){
        List<Integer> collect = transactions.stream().filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity())).map(Transaction::getValue).collect(Collectors.toList());
        System.out.println(collect);

    }

    /**
     * 所有交易中，最高的交易额是多少
     */
    @Test
    public void test7(){
        Optional<Integer> result = transactions.stream().map(Transaction::getValue).reduce(Integer::max);
        result.ifPresent(System.out::println);

    }

    /**
     * 最小
     */
    @Test
    public void test8(){
        Optional<Integer> result = transactions.stream().map(Transaction::getValue).reduce(Integer::min);
        result.ifPresent(System.out::println);

    }

    /**
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception{

        Map<Integer, List<Transaction>> collect = transactions.stream().collect(Collectors.groupingBy(Transaction::getValue));


    }
}
